package cgl.sensorgrid.sopac.gps.filters;

import cgl.narada.event.NBEvent;
import cgl.sensorgrid.filters.Filter;
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
import cgl.narada.service.time.TimeService;
import cgl.narada.service.time.ntptime.TimeServiceImpl;


public class RYOPublisher extends Filter {

    private String ryoFilePath;
    private long publishFrequency;
    private static int msgcount=0;
    private TimeService t;

    public RYOPublisher(String[] args, String[] argsb) {
        if (args == null || args.length == 0) {
            System.err.println(
                    "Incorrect number of arguments in ryo2ascii::Constructor...");
        } else if (argsb.length == 2) {

            //init the NTP service
            t = TimeServiceImpl.getInstance();

            //Sleep for 5 seconds after initializing the NTP service
            try {
                System.out.println(
                        "Sleeping for 3 seconds before initializing the NTP service");
                Thread.sleep(3000);
            } catch (InterruptedException ex1) {
                ex1.printStackTrace();
            }

            if (argsb[0] != null && argsb[0].length() > 0)
                this.ryoFilePath = (argsb[0]);

            //How many messages to be published in one second
            if (argsb[1] != null && argsb[1].length() > 0)
                this.publishFrequency = 1000 / Long.parseLong(argsb[1]);

            this.initialize(args);
            publishRYOFile(ryoFilePath);

        }
    }

    public void publishRYOFile(String filePath) {
        String line = "";
        try {

            //Finding EOF markers inside the RYO File to determine the end
            //of individual network messages.

            DataInputStream dis = new DataInputStream(new FileInputStream(
                    filePath));

            int start = 0;
            int markerIndex = 0;
            byte[] by = new byte[2048];
            byte[] message = new byte[2048];

            Charset charset = Charset.forName("ISO-8859-1");
            CharsetDecoder decoder = charset.newDecoder();
            CharSequence eofMarker = new String("[EOF]");

            //System.out.println("Remaining bytes to be published: ");

            while (dis.available() > 0) {

                //System.out.print(dis.available()+"\t");

                if (dis.available() > 2048)
                    by = new byte[2048];
                else
                    by = new byte[dis.available()];

                dis.read(by);

                ByteBuffer bbuf = ByteBuffer.wrap(by);

                CharBuffer cbuf = decoder.decode(bbuf);
                String s = cbuf.toString();

                if (s.contains(eofMarker)) {
                    //markerIndex is the first index of EOF marker from the beginning of the dis,
                    //hence the lengh of the byte array to hold this particular GPS message

                    markerIndex = s.indexOf("[EOF]");
                    message = new byte[markerIndex];

                    //copy the message from the current dis cursor to markerIndex.
                    System.arraycopy(by, 0, message, 0, markerIndex);

                    //Create the event property stamp
                    msgcount++;
//                    String msgstamp =  msgcount + "\t" + message.length + "\t" + t.getTimestamp();
                    String msgstamp =  msgcount + "\t" + message.length + "\t" + System.currentTimeMillis();

                    //Here publish the data
                    this.publishDataWithProperty(message, "MSGSTAMP", msgstamp);

                    //System.out.println("Sleeping for " + this.publishFrequency + " milisecs..");
                    Thread.sleep(this.publishFrequency);

                    //System.out.println("markerIndex  = " + markerIndex );

                    //move the cursor to the new message
                    start = start + markerIndex + 6;
                    //System.out.println("start = " + start);

                    //close this dis
                    dis.close();

                    //open a new dis and skip the already published messages
                    dis = new DataInputStream(new FileInputStream(filePath));
                    dis.skipBytes(start);
                }
            }
        } catch (Exception ex) {
            System.out.println(line);
            ex.printStackTrace();
        }
    }


    public void onEvent(NBEvent nbEvent) {
    }

    public static void main(String args[]) {
        if (args.length < 5) {
            System.out.println("Usage: java cgl.sensorgrid.sopac.gps.filters.RYOPublisher commType nbHost nbPort pubTopic ryoFilePath publishFrequency");
            System.exit(0);
        }

        String commType = args[0];
        String nbHost = args[1];
        String nbPort = args[2];
        String pubTopic = args[3];
        String ryoFilePath = args[4];
        String publishFrequency = args[5];

//        String commType = "tcp";
//        String nbHost = "gf3.ucs.indiana.edu";
//        String nbPort = "5544";
//        String pubTopic = "/SOPAC/GPS/CRTN_01/RYO1";
//        String ryoFilePath =
//                "C:/home/gaydin/sensorgrid/data/ryo/CRTN_01-09_10_2006-05_05_17_PM.ryo";
//        String publishFrequency = "1";

        String[] argsa = {commType,
                         nbHost,
                         nbPort,
                         "",
                         pubTopic};

        String[] argsb = {ryoFilePath, publishFrequency};
        RYOPublisher publish = new RYOPublisher(argsa, argsb);
    }
}
